import process from "process";
import * as step from '@flow-step/step-toolkit'

interface CheckRedline {
    key: string;
    type: string;
    threshold: number;
}

export function redlineCheckResult(redlines: CheckRedline[], key: string, value: number): boolean {
    const redline = redlines.find(rl => rl.key === key);

    // 如果没有找到相应的检查红线配置，则退出函数
    if (!redline) return true;

    const { type, threshold } = redline;
    let checkResult: boolean;

    switch (type) {
        case 'ge': // 大于等于
            checkResult = value >= threshold;
            break;
        case 'eq': // 等于
            checkResult = value === threshold;
            break;
        case 'le': // 小于等于
            checkResult = value <= threshold;
            break;
        default: // 如果 type 不匹配，退出函数
            return false;
    }

    // 如果检查结果为false，输出相关信息
    step.info(`[${process.env['stepIdentifier']}]REDLINE_ITEM_LINE:{"key":"${key}","threshold":${threshold},"checkVal":${value},"type":"${type}","checked":true,"checkResult":${checkResult}}`);
    let output = `[${process.env['stepIdentifier']}]REDLINE_ITEM_LINE:{"key":"${key}","threshold":${threshold},"checkVal":${value},"type":"${type}","checked":true,"checkResult":${checkResult}}`
    step.addOutput(output)
    return checkResult;

}

export function redlineCheck(key: string,title: string,value: number,style: string): void{


    let outputName = `[${process.env['stepIdentifier']}]STAT_NAME_${key}:${title}`;
    step.addOutput( outputName)
    step.info(outputName)
    let outputValue = `[${process.env['stepIdentifier']}]STAT_VALUE_${key}:${value}`;
    step.addOutput( outputValue)
    step.info(outputValue)
    let outputStyle = `[${process.env['stepIdentifier']}]STAT_STYLE_${key}:${style}`;
    step.addOutput( outputStyle)
    step.info(outputStyle)
    const checkRedlines = process.env["CHECK_REDLINES"]
    if(!checkRedlines){
        return
    }
    redlineCheckResult(JSON.parse(checkRedlines),key,value);
}

export function reportResultUrl(reportUrl: string): void{
    let outputReport = `[${process.env['stepIdentifier']}]STAT_URL__REPORT:${reportUrl}`;
    step.info(outputReport)
    step.addOutput(outputReport)
}
